package com.nj.liandi.gulimall.order.config;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

@Configuration
public class MyRabbitConfig {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Bean
    public MessageConverter messageConverter(){

        return new Jackson2JsonMessageConverter();
    }

    @PostConstruct //MyRabbitConfig这个对象创建完成之后,执行这个方法
    public void initRabbitTemplate(){
        rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
            @Override
            public void confirm(CorrelationData correlationData, boolean b, String s) {
                System.out.println("1)"+correlationData+"2)"+b+"3"+s);
            }
        });
/*        private final Message message;投递失败的详细信息
        private final int replyCode;回复的状态码
        private final String replyText;回复的文本内容
        private final String exchange;当时这个消息发给那个交换机
        private final String routingKey;当时这个消息用那个路由键*/
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override//消息没有投递给指定的队列是促发这个失败回调
            public void returnedMessage(ReturnedMessage returnedMessage) {

            }
        });
    }
}
